package com.firefightingiot_data.data.support.utils.hikvision;

import cn.hutool.json.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: YangYu
 * @ClassName: HikvisionInterfaceCall
 * @Date: Created in 16:18 2021/12/7
 * @Description: 海康接口调用工具类
 */
public class HikvisionInterfaceCall {

    //视频预览
    public static String GetCameraPreviewURL(String number, String url, String protocol) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = "122.4.227.102:4433"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "24769112";  // 秘钥appkey
        ArtemisConfig.appSecret = "NxjYIfempoqQaVtRqKLF";// 秘钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        /*JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", number);
        jsonBody.put("protocol", protocol);
        jsonBody.put("streamType", 0);
        jsonBody.put("transmode", 0);*/
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", number);
        jsonBody.put("streamType", 0);
        jsonBody.put("protocol", protocol);
        jsonBody.put("transmode", 1);
        jsonBody.put("expand", "transcode=0");
        jsonBody.put("streamform", "ps");
//        if (protocol.equals("rtsp")){
//            jsonBody.put("expand", "streamform=rtp");
//        }
        String body = jsonBody.toString();
        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        return result;
    }

    //海康录像回放
    public static String GetHKPlayBackURL(String number, String url, String protocol,String beginTime, String endTime) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = "122.4.227.102:4433"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "24769112";  // 秘钥appkey
        ArtemisConfig.appSecret = "NxjYIfempoqQaVtRqKLF";// 秘钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("cameraIndexCode", number);
        jsonBody.put("protocol", protocol);
        jsonBody.put("streamType", 0);
        jsonBody.put("transmode", 0);
        jsonBody.put("beginTime", StringToIos8601(beginTime));
        jsonBody.put("endTime", StringToIos8601(endTime));
//        if (protocol.equals("rtsp")){
//            jsonBody.put("expand", "streamform=rtp");
//        }
        String body = jsonBody.toString();
        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        return result;
    }

    /**
     * @Author YangYu
     * @Description 海康梯控接口调用组装
     * @Date Created in 16:12 2021/12/7
     * @Param [url, eventTypes, eventDest, jsonBody]
     */

    public static String eventSubscriptionsHK(String url,JSONObject jsonBody) {

        /**
         * STEP1：设置平台参数，根据实际情况,设置host appkey appsecret 三个参数.
         */
        ArtemisConfig.host = "122.4.227.102:4433"; // artemis网关服务器ip端口
        ArtemisConfig.appKey = "24769112";  // 秘钥appkey
        ArtemisConfig.appSecret = "NxjYIfempoqQaVtRqKLF";// 秘钥appSecret

        /**
         * STEP2：设置OpenAPI接口的上下文
         */
        final String ARTEMIS_PATH = "/artemis";

        /**
         * STEP3：设置接口的URI地址
         */
        final String previewURLsApi = ARTEMIS_PATH + url;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);//根据现场环境部署确认是http还是https
            }
        };

        /**
         * STEP4：设置参数提交方式
         */
        String contentType = "application/json";

        /**
         * STEP5：组装请求参数
         */
        String body = jsonBody.toString();
        /**
         * STEP6：调用接口
         */
        String result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);// post请求application/json类型参数
        return result;
    }
    /**
     * String类型的时间(yyyy-MM-dd HH:mm:ss)转换成IOS-8601格式
     * @param data
     * @return
     */
    public static String StringToIos8601(String data) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dt1 = LocalDateTime.parse(data, dateTimeFormatter);
        ZoneOffset offset = ZoneOffset.of("+08:00");
        OffsetDateTime date = OffsetDateTime.of(dt1, offset);
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        String datas = date.format(formatter2);
        return datas;
    }
}
